#随机从mysql数据库中拿一个ip

import pymysql
from queue import Queue

class PM(object):
    def __init__(self):
        #连接数据库
        self.conn = pymysql.connect('10.15.112.30', 'bingbing', 'a11112222', 'jobweb', charset='utf8')
        #创建游标
        self.cursor = self.conn.cursor()
        #创建列队
        self.proxy_q = Queue()

    def get_all_proxy(self):
        #从数据库查询所有的ip
        self.cursor.execute('select * from proxypool')
        res = self.cursor.fetchall()
        #把查询出的ip循环加入列队
        for proxy in res:
            self.proxy_q.put(proxy)

    def filter_proxy(self):
        #队列非空的情况下进行whiile循环
        while not self.proxy_q.empty():
            #把ip从队列里取出来
            p = self.proxy_q.get()
            base_url = 'http://www.baidu.com/s?wd=ip'
            proxy = {
                'http': 'http://%s:%s' % (p[1], p[2]),
                'https': 'http://%s:%s' % (p[1], p[2])
            }
            try:
                response = requests.get(base_url, proxies=proxy, timeout=10)
                #判断响应码
                if 200 <= response.status_code < 300:
                    html = response.text
                    if '本机IP' in html:
                        print(p[1], '可用------------------')
                    else:
                        # 删除代理
                        self.drop_proxy(p)
                else:
                    # 删除代理
                    self.drop_proxy(p)
            except Exception as e:
                # 删除代理
                self.drop_proxy(p)
    #封装删除代理的函数
    def drop_proxy(self, p):
        #删除sql语句
        sql = 'delete from proxypool where id=%s' % p[0]
        try:
            row = self.cursor.execute(sql)
            self.conn.commit()
            print('删除代理：', p[1])
        except Exception as e:
            print('删除代理失败')

    def close(self):
        #关闭数据库
        self.cursor.close()
        self.conn.close()

    def main(self):
        # 加载代理
        self.get_all_proxy()
        # 生成协程对象
        g_list = []
        for i in range(2):
            g = gevent.spawn(self.filter_proxy)
            g_list.append(g)
        # 启动
        gevent.joinall(g_list)

    def random(self):
        #随机读取一个ip
        sql = 'select * from proxypool order by rand() limit 1'
        self.cursor.execute(sql)
        proxy = self.cursor.fetchone()
        return proxy


if __name__ == '__main__':
    from gevent import monkey

    monkey.patch_all()

    import gevent
    import time
    import requests

    pm = PM()
    pm.main()
    # pm.random()
    pm.close()
